import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import {HttpClient} from '@angular/common/http';
import {any} from 'codelyzer/util/function';
@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
  //
  doRemove = {
    type: true,
    str: '手机号不能为空',
    curr: false
  };
  doP = {
    type: true,
    str: '请输入密码',
    curr: false
  };
  doY = {
    type: true,
    str: '请输入验证码',
    curr: false
  };
  // 后台给的手机验证码
  telObj: Array<Obj>;
  yz: any;
  str = '';
  pwd: any;
  // 输入的手机验证码
  yzS: any;
  // 提示消息
  tiShi: String;
  // 允许跳转页面
  promise: Array<any>;
  constructor (private http: HttpClient) { }
  ngOnInit() {
  }
  // 注册时发送验证码
  doClick(e) {
    if (this.str !== '') {
      if ((/^1[3|4|5|8][0-9]\d{4,8}$/.test(this.str))) {
        // 发情请求
        this.http.get('/app/get', {params: {['tel']: this.str}}).subscribe((data) => {
          // console.log(data.obj);
          this.yz = data['obj'];
        });
      } else {
        alert('请输入正确的手机号');
      }

    } else {
      alert('请输入手机号');
    }
    console.log(this.str);
  }
  // 点击注册时
  doRegister($event) {
    if (this.doRemove.curr) {
      if (this.doP.curr) {
        if (this.doY.curr) {
          // if ($('#do').get(0).checked === true) {
          //   this.http.get('/account/get', {params: {['tel']: this.str, ['pwd']: this.pwd}}).subscribe((data) => {
          //    if (data['name'] === '注册成功') {
          //      this.promise = ['/login'];
          //    }
          //   });
          // } else {
          //   alert('请阅读协议');
          // }
        } else {
          this.doby();
        }
      } else {
        this.dobp();
      }
    } else {
      this.doBlur();
    }

  }
  // 获得焦点
  doFocus() {
    this.doRemove.type = false;
  }
  doPwd() {
    this.doP.type = false;
  }
  doYzm() {
    this.doY.type = false;
  }
  // 失去焦点
  doBlur() {
    if (this.str !== '') {
      if ((/^1[3|4|5|8][0-9]\d{4,8}$/.test(this.str))) {
        this.doRemove.type = false;
        this.doRemove.curr = true;
      } else {
        this.doRemove.type = true;
        this.doRemove.str = '手机号码不正确，请重新输入';
      }
    } else {
      this.doRemove.type = true;
    }

  }

  dobp() {
    if (this.pwd !== '') {
      if((/^\w{6,16}$/.test(this.pwd))) {
        this.doP.type = false;
        this.doP.curr = true;
      } else {
        this.doP.type = true;
        this.doP.str = '密码简单，请重新输入';
      }
    } else {
      this.doP.type = true;
    }
  }
  doby() {
    if (this.yzS !== '') {
      if ((/^\d{4}$/.test(this.yzS))) {
        // 判断手机号验证码是否正确
        if (this.yzS === this.yz) {
          this.doY.type = false;
          this.doY.curr = true;
        } else {
          console.log('aasww');
          this.doY.type = true;
          this.doY.str = '请输入正确的验证码';
        }
      } else {
        console.log('aass')
        this.doY.type = true;
        this.doY.str = '请输入正确的验证码';
      }
    } else {
      this.doY.type = true;
    }
  }

}

// 自定义一个接口
export interface Obj {
  code: String;
  msg: String;
  obj: String;
}
